﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using EntityObjects;
namespace PresentationLayer.View
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        static string po;
        UpdateStockSupplierControl c;
        protected void Page_Load(object sender, EventArgs e)
        {
             po = Request.QueryString.ToString();
            if (!IsPostBack)
            {
                btnSubmit.Enabled = false;
                lblDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                c = new UpdateStockSupplierControl();
                List<PurchaseOrderDetails> lst = c.getOrderDetails(po);
                foo.DataSource = c.getOrderDetails(po);
                foo.DataBind();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvr in foo.Rows)
            {
                string data1 = gvr.Cells[1].Text;    //get data from BoundField in each row
                string odrQty = gvr.Cells[2].Text;
                string data2 = ((TextBox)gvr.FindControl("tbQty")).Text;    //get data from control in TemplateField in each row
                c = new UpdateStockSupplierControl();
                bool flag=c.UpdateData(data1, Convert.ToInt16(data2),po);
                if (flag != false) {
                    lblSuccess.Text = "Update Successful!";
                }
                else lblSuccess.Text = "Update Failed!";
                lblSuccess.Visible = true;
                    btnCancel.Enabled = false;
                btnSubmit.Enabled = false;
                              
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Server.Transfer("UpdateStockFromSupplier1.aspx");
        }

        protected void tbQty_TextChanged(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            GridViewRow thisRow = (GridViewRow)txt.Parent.Parent;
            string odrQty = thisRow.Cells[2].Text;
            string rdQty= ((TextBox)thisRow.FindControl("tbQty")).Text;
              if (Convert.ToInt16(odrQty) >= Convert.ToInt16(rdQty))
                {
                lblErr.Visible = false;
                btnSubmit.Enabled = true;
                }
              else lblErr.Visible = true;
             
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {           
            Server.Transfer("UpdateStockFromSupplier1.aspx");
        }       
    }
}